package com.woniuxy.controller;

import com.woniuxy.bean.RoleBean;
import com.woniuxy.pojo.User;
import com.woniuxy.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("goList.html")
    public String goUserList(@RequestParam(name = "currentPage",defaultValue = "1") Integer currentPage,
                             @RequestParam(defaultValue = "4") Integer pageSize,
                             String userName,
                             HttpServletRequest request){
        request.setAttribute("userName",userName);
        if(userName==null|| "".equals(userName)){
            userName = "%";
        }

        Integer totalSize = userService.getTotalSize(userName);
        Integer pages = userService.getPages(totalSize, pageSize);
        if (currentPage > pages){
            currentPage = pages;
        }
        if (currentPage<1){
            currentPage=1;
        }
        List<User> userList = userService.getUserListByPage(currentPage, pageSize,userName);
        request.setAttribute("totalSize",totalSize);
        request.setAttribute("pages",pages);
        request.setAttribute("userList",userList);
        request.setAttribute("currentPage",currentPage);
        request.setAttribute("pageSize",pageSize);

        return "user_goList";
    }
    @RequestMapping("goUserRoles.html")
    public String goUserRoles(Integer userId,HttpServletRequest request){
        List<RoleBean> roleBeansList = this.userService.getRoleBeansByUserId(userId);
        request.setAttribute("roleBeansList",roleBeansList);
        request.setAttribute("userId",userId);
        return "user_role";
    }
    @RequestMapping("assignRole.html")
    @ResponseBody
    public Boolean assignRole(@RequestParam List<Integer> userRole,Integer userId){
        Boolean flag = this.userService.assighUserRole(userId, userRole);
        return flag;
    }
    @RequestMapping("goUserAdd.html")
    public String goUserAdd(){
        return "user_add";
    }
    @RequestMapping("add.html")
    public String add(User user){
        userService.add(user);
        return "redirect:goList.html";
    }
    @RequestMapping("deleteUser.html")
    public String add(Integer id){
        userService.delete(id);
        return "redirect:goList.html";
    }
    @RequestMapping("goUpdUser.html")
    public String findOne(Integer id, Model model){
        User user = userService.findOne(id);
        model.addAttribute("user",user);
        return "user_upd";
    }
    @RequestMapping("updUser.html")
    public String upd(User user){
        userService.upd(user);
        return "redirect:goList.html";
    }

}
